#include "video-text.h"

/** Calculate memory offset for writing at position x, y
*/
ulong calcMemPosition(int x, int y)
{
	return VIDEO_TEXT + y * (WIDTH * 2) + x * 2;
}

/** Print character ch at position x,y with attributes
* return true if sucessfull
*/
Bool printCharAt(char ch, int x, int y, char attributes)
{
	if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) return false;
	
	_farsetsel(_dos_ds);
	_farnspokeb(calcMemPosition(x, y), ch);
	_farnspokeb(calcMemPosition(x, y) + 1, attributes);
	return true;
}


/** Clear screen with background color
*/
void fillScreen(char background)
{
	int i, j;
	for(i = 0; i < WIDTH; i++)
		for(j = 0; j < HEIGHT; j++)
			printCharAt(0, i, j, background);
}

/** Print character array str at position x,y with attributes
* return true if sucessfull
*/
Bool printStringAt(char *str, int x, int y, char attributes)
{
	int i = 0;
	while(str[i] != 0)
	{
		if(!printCharAt(str[i], x + i, y, attributes)) return false;
		i++;
	}
		
	return true;
}

/** Print integer num at position x,y with attributes.
* No C library function is used.
* return true if sucessfull
*/
Bool printIntAt(int num, int x, int y, char attributes)
{
	int i = 0;
	
	//se o numero for negativo, imprimir '-'
	if(num < 0)
	{
		if(!printCharAt('-', x, y, attributes)) return false;
		i++;
		num = 0 - num;
	}
	
	//descobrir a ordem de grandeza do numero
	int ordem = 1;
	while(num / ordem >= 10) ordem *= 10;
	
	//da esquerda para a direita, imprimir os algarismos
	while(1)
	{
		int digito = num / ordem;
		if(!printCharAt(digito + 0x30, x + i, y, attributes)) return false;
		num -= digito * ordem;
		if(ordem == 1) break;
		ordem /= 10;
		i++;
	}

	return true;
}


/** Draw rectangular frame with left upper corner at position x,y
* with width and height dimensions, decorated with title attributes.
* return true if sucessfull
*/
Bool drawFrame(char *title, char attributes, int x, int y, int width, int height)
{
	int x_fin = x + width - 1;
	int y_fin = y + height - 1;
	
	if(!printCharAt(0xC9, x, y, attributes)) return false;			//canto superior esquerdo
	if(!printCharAt(0xBB, x_fin, y, attributes)) return false;		//canto superior direito
	if(!printCharAt(0xC8, x, y_fin, attributes)) return false;		//canto inferior esquerdo
	if(!printCharAt(0xBC, x_fin, y_fin, attributes)) return false;	//canto inferior direito
	
	int i = x + 1;
	for( ; i < x_fin; i++)
	{
		printCharAt(0xCD, i, y, attributes);						//margem superior
		printCharAt(0xCD, i, y_fin, attributes);					//margem inferior	
	}
	
	i = y + 1;
	for( ; i < y_fin; i++)
	{
		printCharAt(0xBA, x, i, attributes);						//margem esquerda
		printCharAt(0xBA, x_fin, i, attributes);					//margem direita
	}
	
	//verificar se o titulo cabe na moldura
	int title_size = 0;
	while(title[title_size] != 0) title_size++;
	if(title_size > width - 2) return false;
	
	printStringAt(title, x+width/2-title_size/2, y , attributes);					//titulo
	return true;
}
